package com.aaa.ssm.service.impl;

import com.aaa.ssm.dao.TNewsDao;
import com.aaa.ssm.entity.TDicNechannel;
import com.aaa.ssm.entity.TNews;
import com.aaa.ssm.service.TNewsService;
import com.aaa.ssm.util.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class TNewsServiceImpl implements TNewsService {
    @Resource
    private TNewsDao tNewsDao;
    @Resource
    HttpSession session;

    private static String time(){
        //获取当前时间
        Date date = new Date();
        //实例化格式化时间类
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //format格式化时间
        String format = simpleDateFormat.format(date);
        return format;
    }

    @Override
    public PageInfo queryPage(Page<TNews> page) {
        PageHelper.startPage(page.getPageNo(),page.getPageSize());
        return new PageInfo(tNewsDao.queryParam(page.getData()));

    }

    /**
     * 两表分页带参查询
     * @param page
     * @return
     */
    @Override
    public PageInfo queryAll(Page<TDicNechannel> page) {
        PageHelper.startPage(page.getPageNo(),page.getPageSize());
        return new PageInfo(tNewsDao.queryAll(page.getData()));
    }

    @Override
    public TNews queryById(int id) {
        List<TNews> tNewsList=tNewsDao.queryById(id);
        if (tNewsList!=null&&tNewsList.size()>0){
            return tNewsList.get(0);
        }
        return null;
    }

    @Override
    public int add(TNews tNews) {
        String startegyA = (String) session.getAttribute("loginA");
        tNews.setCjtime(time());
        tNews.setCjrid(startegyA);
        return tNewsDao.add(tNews);
    }

    @Override
    public int update(TNews tNews) {
        try {
            String startegyA = (String) session.getAttribute("loginA");
            tNews.setUpdatetime(time());
            tNews.setUpdaterid(startegyA);
             tNewsDao.update(tNews);
             return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteById(int id) {
        return tNewsDao.deleteById(id);
    }
}
